package com.wlc.conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBConnection {
	
		/*String url="jdbc:oracle:thin:@localhost:1521:orcl";
		String sql="select * from users";
		Connection conn=null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection(url,"scott","Oracle11g");
			System.out.println("database connection success!");
			PreparedStatement psmt=conn.prepareStatement(sql);
			ResultSet rs=psmt.executeQuery();
			while(rs.next())
			{
				System.out.print(rs.getInt(1)+" ");
				System.out.print(rs.getString(2)+" ");
				System.out.print(rs.getString(3)+" ");
				System.out.println(rs.getString(4)+" ");
			}
		} catch (ClassNotFoundException e ) {
			
		System.out.println("Class not found,error message is "+e.getMessage());	
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		private final static String url="jdbc:oracle:thin:@localhost:1521:orcl";
		private final static String user="scott";
		private final static String password="Oracle11g";
		
	static{
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Class not found,error message is"+e.getMessage());
		}
		
	}
	
	public static Connection getConnection() {
		Connection connection =null;
		try {
			connection=DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	public void closeConnection(Connection connection){
		if(null!=connection)
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
}
